#!/usr/bin/env python
import os
import utils
import xmmsclient.glib
from gtk import *
from gtk.glade import XML
from xmmsclient import XMMS
from broadcastman import BroadcastManager

class Aidem(XML):
    def __init__(self, xmms2):
        XML.__init__(self, "aidem.glade")
        self.signal_autoconnect(self)
        utils.build_playlist(self)
        utils.configure_progressbar(self)
        self.xmms2 = xmms2
        treeview = self.get_widget("treeview1")
        self.selection = treeview.get_selection()
        BroadcastManager(self, xmms2)

    def clear(self, *args):
        self.xmms2.playlist_clear()
        
    def hide_filter(self, button):
        filter = self.get_widget("filterbox")
        filter.hide()
        
    def next(self, *args):
        self.xmms2.playlist_set_next_rel(1)
        self.xmms2.playback_tickle()

    def on_progressbar_pressed(self, progressbar, event):
        self.progressbar_pressed = True
        
    def on_progressbar_released(self, progressbar, event):
        self.progressbar_pressed = False
        playtime = progressbar.get_value()
        self.xmms2.playback_seek_ms(playtime)

    def on_row_activated(self, treeview, path, column):
        position, = path
        self.xmms2.playlist_set_next(position)
        self.xmms2.playback_tickle()
        
    def play_pause(self, *args):
        self.xmms2.playback_start()
        
    def previous(self, *args):
        self.xmms2.playlist_set_next_rel(-1)
        self.xmms2.playback_tickle()
        
    def show_filter(self, menuitem):
        filter = self.get_widget("filterbox")
        filter.show()
        
    def quit(self, *args):
        main_quit()
        
    def remove(self, *args):
        model, rows = self.selection.get_selected_rows()
        rows.reverse()
        for id, in rows:
            self.xmms2.playlist_remove_entry(id)


xmms2 = XMMS("aidem")
try:
    xmms2.connect(os.getenv("XMMS_PATH"))
except Exception, e:
    print "[xmms2 client]: Could not connect to xmms2d"
    print e
    print "Aborting!"
    exit(1)
xmmsclient.glib.GLibConnector(xmms2)

Aidem(xmms2)
main()
